package com.yuanlrc.hotel.restaurant.controller;

import java.util.List;

import com.yuanlrc.hotel.restaurantPicture.domain.RestaurantPicture;
import com.yuanlrc.hotel.restaurantPicture.service.IRestaurantPictureService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yuanlrc.common.annotation.Log;
import com.yuanlrc.common.enums.BusinessType;
import com.yuanlrc.hotel.restaurant.domain.Restaurant;
import com.yuanlrc.hotel.restaurant.service.IRestaurantService;
import com.yuanlrc.common.core.controller.BaseController;
import com.yuanlrc.common.core.domain.AjaxResult;
import com.yuanlrc.common.utils.poi.ExcelUtil;
import com.yuanlrc.common.core.page.TableDataInfo;

/**
 * 餐厅管理Controller
 * 
 * @author yuanlrc
 * @date 2020-11-19
 */
@Controller
@RequestMapping("/hotel/restaurant")
public class RestaurantController extends BaseController
{
    private String prefix = "hotel/restaurant";

    @Autowired
    private IRestaurantService restaurantService;

    @Autowired
    private IRestaurantPictureService iRestaurantPictureService;

    @RequiresPermissions("hotel:restaurant:view")
    @GetMapping()
    public String restaurant()
    {
        return prefix + "/restaurant";
    }

    /**
     * 查询餐厅管理列表
     */
    @RequiresPermissions("hotel:restaurant:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Restaurant restaurant)
    {
        startPage();
        List<Restaurant> list = restaurantService.selectRestaurantList(restaurant);
        return getDataTable(list);
    }

    /**
     * 导出餐厅管理列表
     */
    @RequiresPermissions("hotel:restaurant:export")
    @Log(title = "餐厅管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Restaurant restaurant)
    {
        List<Restaurant> list = restaurantService.selectRestaurantList(restaurant);
        ExcelUtil<Restaurant> util = new ExcelUtil<Restaurant>(Restaurant.class);
        return util.exportExcel(list, "restaurant");
    }

    /**
     * 新增餐厅管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存餐厅管理
     */
    @RequiresPermissions("hotel:restaurant:add")
    @Log(title = "餐厅管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Restaurant restaurant)
    {
        Restaurant restaurant1 = restaurantService.selectRestaurantByName(restaurant.getName());
        if (restaurant1!=null){
            return error("餐厅名不能重复!");
        }
        if (restaurant.getName()==null||restaurant.getName().length()==0||restaurant.getName()==" "){
            return error("餐厅名不能为空!");
        }else if (restaurant.getDescrib().length()<10){
            return error("餐厅介绍内容过短!");
        }
        int count = 0;
        try{
            count = restaurantService.insertRestaurant(restaurant);
        }catch (Exception e){
            return toAjax(false);
        }
        return toAjax(count);
    }

    /**
     * 修改餐厅管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Restaurant restaurant = restaurantService.selectRestaurantById(id);
        mmap.put("restaurant", restaurant);
        return prefix + "/edit";
    }

    /**
     * 修改保存餐厅管理
     */
    @RequiresPermissions("hotel:restaurant:edit")
    @Log(title = "餐厅管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Restaurant restaurant)
    {
        if (restaurant.getName()==null||restaurant.getName().length()==0||restaurant.getName()==" "){
            return error("餐厅名不能为空");
        }else if (restaurant.getDescrib().length()<10){
            return error("餐厅介绍内容过短");
        }
        int count = 0;
        try{
            count=restaurantService.updateRestaurant(restaurant);
        }catch (Exception e){
            return toAjax(false);
        }
        return toAjax(count);
    }

    /**
     * 删除餐厅管理
     */
    @RequiresPermissions("hotel:restaurant:remove")
    @Log(title = "餐厅管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        int count = 0;
        try {
            count=restaurantService.deleteRestaurantByIds(ids);
        }catch (Exception e){
            return error("存在关联数据,不可删除");
        }
        return toAjax(count);
    }
}
